package com.shigui.news.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shigui.news.dao.UserDao;
import com.shigui.news.enums.ResponseCode;
import com.shigui.news.exception.UserException;
import com.shigui.news.model.User;
import com.shigui.news.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	
	
	
	@Override
	public User selectById(Integer id) {
		// TODO Auto-generated method stub
		return userDao.selectById(id);
	}

	@Override
	public List<User> select() {
		// TODO Auto-generated method stub
		return userDao.select();
	}

	@Override
	public void insert(User user) {

		userDao.insert(user);
	}

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		userDao.update(user);
	}

	@Override
	public void delete(User user) {
		// TODO Auto-generated method stub
		userDao.delete(user);
	}

	@Override
	public User login(String username, String password) {
		// TODO Auto-generated method stub
		User user = userDao.findByUser(username, password);
		
		if(user == null){
			
			throw new UserException(ResponseCode.USER_LOGIN_FAIL);
		}
		
		user.createPersistCode();
		
		userDao.update(user);
		
		return user;
	}

	@Override
	public void logout() {
		// TODO Auto-generated method stub
		userDao.logout();
	}

	
}
